﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Linq.Expressions;
using System.Text;
using System.Data.Entity;
using padnug.Domain;
using PadnugFramework;
using PadnugRepositoryInterfaces;

namespace PadnugRepository
{
    public class PadnugRoleRepository : IPadnugRoleRepository
    {
        private padnugEntities db;
        public PadnugRoleRepository()
        {
            db = new padnugEntities();
        }
        #region IPadnugRoleRepository Members

        public bool IsUserInRole(string username, string roleName)
        {
            return
                db.Users.Any(
                    u => u.Email.ToLower().CompareTo(username.ToLower()) == 0 && u.Role.CompareTo(roleName) == 0);
        }

        public string[] GetRolesForUser(string username)
        {
            string[] roles =
                db.Users.Where(u => u.Email.ToLower().CompareTo(username.ToLower()) == 0).Select(u => u.Role).ToArray();
            return roles;
        }

        public bool RoleExists(string roleName)
        {
            return db.Users.Select(u => u.Role).Distinct().Any(u => u.CompareTo(roleName) == 0);
        }

        #endregion
    }
}
